//
//  SASettingController.m
//  ShopAssistant
//
//  Created by ZhaoYiding on 2017/11/18.
//  Copyright © 2017年 hyxt. All rights reserved.
//

#import "SASettingController.h"
#import "SALoginController.h"

@interface SASettingController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation SASettingController

- (void)viewDidLoad
{
    [super viewDidLoad];
  
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    
    
    [self buildNav];
}

- (void)buildNav
{
    self.title = @"设置";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_arrow"] style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(popViewControllerAnimated:)];
}

#pragma mark - ITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = @[@"当前版本",@"注销"][indexPath.row];
    
    if (indexPath.row == 0)
    {
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        cell.detailTextLabel.text = SACurrentVersion;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1)
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        [MBProgressHUD showMessage:@"请稍后..."];
        
        [SAHttpApi requestLogOutWithPhone:[SAUserDefaults objectForKey:@"SAPhone"] WithComeBack:^(NSDictionary *object, NSError *error) {
            [MBProgressHUD hideHUD];
            if (!error)
            {
                if ([object[@"status"] integerValue] == 1)
                {
                    SALoginController *vc = [[SALoginController alloc] init];
                    SANavigationController *nav = [[SANavigationController alloc] initWithRootViewController:vc];
                    [[SAUserInfoManager sharedSAUserInfoManager] cleanLoginInfo];
                    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
                    
                }
                else
                {
                    [MBProgressHUD hideHUD];
                    [MBProgressHUD showError:object[@"msg"]];
                }
            }
            else
            {
                [MBProgressHUD hideHUD];
                [UIAlertView showWithTitle:@"提示" message:@"请检查网络设置,稍后再试.." cancelButtonTitle:nil otherButtonTitles:@[@"我知道了"] tapBlock:nil];
            }
        }];
    }
    
}


 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
@end
